package cc.zy.base.common.utils;

import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;


@Slf4j
public class CreaterPdf{

    private static String defaut_path = "d://aa.pdf";
    public void create(Map<String, Object> map) throws Exception{


        log.debug("进入生成订单");
        // 明细二维码信息
        String mxewm = "";
        PdfReader reader = new PdfReader(
                "pdf_mould/font/sfpj.pdf"); // 模版文件目录


        // 生成的pdf文件末尾字符串，防止重复覆盖问题
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String strDate = formatter.format(currentTime);

        // 输出pdf文件
        String strOutFileName = ResourceUtils.getFile("classpath:\\pdf_mould\\out") + "\\"
                + strDate + (new Random()).nextInt(999999) + ".pdf";
        log.info(String.format("输出文件绝对路径 strOutFileName = %s", strOutFileName));

        // 輸出pdf文件流
        FileOutputStream outPdfStream = new FileOutputStream(strOutFileName);

        PdfStamper ps = new PdfStamper(reader, outPdfStream); // 生成的输出流

        Map<String, Integer> alignMap = new HashMap<String, Integer>();
        alignMap.put("khmc", Element.ALIGN_LEFT);
        Map<String, String> dataMap = new HashMap<String, String>();
        dataMap.put("xxmc", "西北工业大学222");
        dataMap.put("pc", "2021");
        dataMap.put("cc", "大学本科");
        dataMap.put("zy", "计算机科学与技术");
        dataMap.put("zy", "计算机科学与技术");
        dataMap.put("khmc", "张三 ");
        dataMap.put("kprq", "2021年01月17日");
        AcroFields s = ps.getAcroFields();

        // 循环页数
        dataMap.put("r1_mcgg", "学费1");
        dataMap.put("r1_dw", "学年");
        dataMap.put("r1_sl", "1");
        dataMap.put("r1_dj", "2500");
        dataMap.put("r1_je", "2500");
        dataMap.put("r1_bz", "正常费用");



        dataMap.put("r2_mcgg", "学费2");
        dataMap.put("r2_dw", "学年2");
        dataMap.put("r2_sl", "1");
        dataMap.put("r2_dj", "2600");
        dataMap.put("r2_je", "2600");
        dataMap.put("r2_bz", "正常费用");

        dataMap.put("hjjexx", "￥2,500.00");

        dataMap.put("hjjedx", "贰仟伍佰元整");

        dataMap.put("jsr", "缴款人1");
        dataMap.put("sk", "收款人1");
        dataMap.put("zd", "制单人1");


        CreatePdfUtil.fontBold(ps, dataMap,alignMap);
        PdfContentByte content = ps.getUnderContent(1);

        Image dbMainImage = Image.getInstance("https://test-quanwu.oss-cn-beijing.aliyuncs.com/img_photo/a211ddda-d02c-4af5-ab72-44e1b5ffc14a.png");
        dbMainImage.scaleAbsolute(93, 93);
        dbMainImage.setAbsolutePosition(324,97);
        content.addImage(dbMainImage);
        ps.setFormFlattening(true); // 这句不能少
        ps.close();
        reader.close();
    }
}
